package com.airn.siege.lion.lib.widget;

import android.view.View;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Objects;

public class ViewManager4Selector {
    private final ArrayList<View> list;
    private int currentPressedIndex = -1;

    public ViewManager4Selector() {
        this.list = new ArrayList<>();
    }

    private void addView(View view) {
        if (!this.list.contains(view)) {
            this.list.add(view);
        }

        if (view.isSelected()) {
            view.setSelected(false);
        }

    }

    public void addView(View... views) {
        for (View v : views) {
            this.addView(v);
        }

    }

    public ArrayList<View> getViewList() {
        return this.list;
    }

    public boolean contains(View view) {
        return this.list.contains(view);
    }

    public void addSelectedView(View view) {
        if (view != null) {
            if (!this.list.contains(view)) {
                this.list.add(view);
            }

            if (this.currentPressedIndex != -1) {
                ((View)this.list.get(this.currentPressedIndex)).setSelected(false);
            }

            this.currentPressedIndex = this.list.size() - 1;
            if (view.isSelected()) {
                view.setSelected(true);
            }

        }
    }

    public void removeAllViews() {
        this.list.clear();
    }

    public boolean selectView(View view) {
        return this.selectView(view, false);
    }

    public boolean selectOrCancel(View view) {
        return this.selectView(view, true);
    }

    private boolean selectView(View view, boolean flag) {
        if (view == null) {
            return false;
        } else {
            boolean result;
            int index = this.list.indexOf(view);
            if (this.currentPressedIndex != -1 && this.currentPressedIndex != index) {
                ((View)this.list.get(this.currentPressedIndex)).setSelected(false);
            }

            if (index == -1) {
                this.addSelectedView(view);
                view.setSelected(true);
                result = true;
                this.currentPressedIndex = index;
            } else if (!view.isSelected()) {
                view.setSelected(true);
                result = true;
                this.currentPressedIndex = index;
            } else if (this.currentPressedIndex == index) {
                if (flag) {
                    ((View)this.list.get(index)).setSelected(false);
                    this.currentPressedIndex = -1;
                    result = false;
                } else {
                    result = true;
                    this.currentPressedIndex = index;
                }
            } else {
                ((View)this.list.get(index)).setSelected(true);
                result = true;
                this.currentPressedIndex = index;
            }

            return result;
        }
    }

    public boolean selectView(int viewId) {
        return this.selectView(viewId, false);
    }

    public boolean selectOrCancel(int viewId) {
        return this.selectView(viewId, true);
    }

    private boolean selectView(int viewid, boolean flag) {
        boolean result;
        View view = this.getViewById(viewid);
        if (view == null) {
            return false;
        } else {
            int index = this.list.indexOf(view);
            if (this.currentPressedIndex != -1 && this.currentPressedIndex != index) {
                ((View)this.list.get(this.currentPressedIndex)).setSelected(false);
            }

            if (!view.isSelected()) {
                view.setSelected(true);
                result = true;
                this.currentPressedIndex = index;
            } else if (index == -1) {
                this.addSelectedView(view);
                view.setSelected(true);
                result = true;
                this.currentPressedIndex = index;
            } else if (this.currentPressedIndex == index) {
                if (flag) {
                    ((View)this.list.get(index)).setSelected(false);
                    result = false;
                    this.currentPressedIndex = -1;
                } else {
                    result = true;
                    this.currentPressedIndex = index;
                }
            } else {
                ((View)this.list.get(index)).setSelected(true);
                result = true;
                this.currentPressedIndex = index;
            }

            return result;
        }
    }

    private View getViewById(int id) {
        Iterator var2 = this.list.iterator();

        View v;
        do {
            if (!var2.hasNext()) {
                return null;
            }

            v = (View)var2.next();
        } while(id != v.getId());

        return v;
    }

    public void cancelSelect() {
        if (this.currentPressedIndex != -1 && this.currentPressedIndex < this.list.size()) {
            ((View)this.list.get(this.currentPressedIndex)).setSelected(false);
        }

        this.currentPressedIndex = -1;
    }

    public boolean selectViewByTag(Object tag) {
        return this.selectViewByTag(tag, false);
    }

    public boolean selectOrCancelByTag(Object tag) {
        return this.selectViewByTag(tag, true);
    }

    private boolean selectViewByTag(Object tag, boolean flag) {
        boolean result = false;
        View view = this.getViewByTag(tag);
        if (view == null) {
            return false;
        } else {
            int index = this.list.indexOf(view);
            if (this.currentPressedIndex != -1 && this.currentPressedIndex != index) {
                ((View)this.list.get(this.currentPressedIndex)).setSelected(false);
            }

            if (!view.isSelected()) {
                view.setSelected(true);
                result = true;
                this.currentPressedIndex = index;
            } else if (index == -1) {
                this.addSelectedView(view);
                view.setSelected(true);
                result = true;
                this.currentPressedIndex = index;
            } else if (this.currentPressedIndex == index) {
                if (flag) {
                    ((View)this.list.get(index)).setSelected(false);
                    this.currentPressedIndex = -1;
                } else {
                    result = true;
                    this.currentPressedIndex = index;
                }
            } else {
                ((View)this.list.get(index)).setSelected(true);
                result = true;
                this.currentPressedIndex = index;
            }

            return result;
        }
    }

    private View getViewByTag(Object tag) {
        Iterator var2 = this.list.iterator();

        View v;
        do {
            if (!var2.hasNext()) {
                return null;
            }

            v = (View)var2.next();
        } while(!tag.equals(v.getTag()));

        return v;
    }

    public View getSelectView() {
        return this.currentPressedIndex != -1 && this.currentPressedIndex < this.list.size() ? (View)this.list.get(this.currentPressedIndex) : null;
    }

    public void removeViewByTag(Object tag) {
        View selectView = this.getSelectView();
        int deleteNum = 0;
        boolean isNeedDel = true;
        Iterator iterator = this.list.iterator();

        while(iterator.hasNext()) {
            View view = (View)iterator.next();
            Object t = view.getTag();
            if (Objects.equals(t, tag)) {
                iterator.remove();
                if (view == selectView) {
                    this.currentPressedIndex = -1;
                } else if (isNeedDel) {
                    ++deleteNum;
                }
            }

            if (view == selectView) {
                isNeedDel = false;
            }
        }

        if (this.currentPressedIndex != -1) {
            this.currentPressedIndex -= deleteNum;
        }

    }
}
